<template>
  <div class="space-rank">
    <a-card title="空间使用排行分析">
      <v-chart :option="options" style="height: 320px; max-width: 100%" :loading="loading" />
    </a-card>
  </div>
</template>
<script setup lang="ts">
import 'echarts'
import VChart from 'vue-echarts'
import { computed, ref, watchEffect } from 'vue'
import { getSpaceRankAnalyzeUsingPost } from '@/api/spaceAnalyzeController.ts'
import { message } from 'ant-design-vue'

interface Porps {
  queryAll?: boolean
  queryPublic?: boolean
  spaceId?: string
}

const props = withDefaults(defineProps<Porps>(), {
  queryAll: false,
  queryPublic: false,
})
const dataList = ref<API.Space[]>([])
const loading = ref(false)
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getSpaceRankAnalyzeUsingPost({
      queryAll: props.queryAll,
      queryPublic: props.queryPublic,
      spaceId: props.spaceId,
      topN: 10, // 默认为10
    })
    dataList.value = res.data
  } catch (e) {
    message.error(e)
  } finally {
    loading.value = false
  }
}
// 图表选项
const options = computed(() => {
  const spaceNames = dataList.value.map((item) => item.spaceName)
  const usageData = dataList.value.map((item) => (item.totalSize / 1024 / 2014).toFixed(2)) // 转为MB
  return {
    tooltip: {
      trigger: 'axis',
    },
    xAxis: {
      type: 'category',
      data: spaceNames,
    },
    yAxis: {
      type: 'value',
      name: '图片使用量(MB)',
    },
    series: [
      {
        name: '图片使用量(MB)',
        type: 'bar',
        data: usageData,
        itemStyle: {
          color: '#5470C6',
        },
      },
    ],
  }
})
// 加载数据
watchEffect(() => {
  fetchData()
})
</script>

<style scoped></style>
